package com.common.domain;



import com.common.constants.code.ResultCode;
import com.util.CollectionUtil;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.LinkedList;

@XmlRootElement(name = "jsonResult")
public class JsonResult {

	private String code;

	private Object result;

	/**
	 * 错误类型@see ResultCode
	 */
	private Integer errorType;

	private String errorMsg;

	private boolean isSuccess = true;

	private JsonResult() {
		this.code = ResultCode.NORMAL.getCode();
		this.errorMsg = ResultCode.NORMAL.getMsg();
	}

	public JsonResult(LinkedList<ResultCode> resultCodeList) {
		if (CollectionUtil.isEmpty(resultCodeList)) {
			this.code = ResultCode.ERROR_SYSTEM.getCode();
			this.errorMsg = ResultCode.ERROR_SYSTEM.getMsg();
		} else {
			for (ResultCode code : resultCodeList) {
				this.code += code.getCode();
				this.errorMsg += code.getMsg();
			}
		}
		this.isSuccess = false;
		this.result = null;
	}

	public JsonResult(ResultCode resultCode) {
		if (resultCode == null) {
			this.code = ResultCode.ERROR_SYSTEM.getCode();
			this.errorMsg = ResultCode.ERROR_SYSTEM.getMsg();
		} else {
			this.code = resultCode.getCode();
			this.errorMsg = resultCode.getMsg();
		}
		this.isSuccess = false;
		this.result = null;
	}

	public JsonResult(Object result) {
		this.code = ResultCode.NORMAL.getCode();
		this.errorMsg = ResultCode.NORMAL.getMsg();
		this.result = result;
	}
	
	public static JsonResult create(ResultCode resultCode) {
		return new JsonResult(resultCode);
	}

	public static JsonResult create() {
		return new JsonResult();
	}
	
	public static JsonResult error(){
		return new JsonResult(ResultCode.ERROR_SYSTEM);
	}

	public Integer getErrorType() {
		return errorType;
	}

	public void setErrorType(Integer errorType) {
		this.errorType = errorType;
	}

	@XmlElement
	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	@XmlElement
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@XmlElement
	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	@XmlElement
	public boolean isSuccess() {
		return isSuccess;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

}
